<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class Coupon extends \app\BaseModel
{
    protected $name = "massage_service_coupon";
    protected $append = ["service", "send_count"];
    public function getSendCountAttr($value, $data)
    {
        if(!empty($data["id"])) {
            $record_model = new CouponRecord();
            $count = $record_model->where(["coupon_id" => $data["id"]])->sum("num");
            return $count;
        }
    }
    public function getServiceAttr($value, $data)
    {
        if(!empty($data["id"])) {
            $ser_model = new Service();
            $dis = ["a.status" => 1, "b.coupon_id" => $data["id"], "b.type" => 0];
            $list = $ser_model->alias("a")->join("massage_service_coupon_goods b", "b.goods_id = a.id")->where($dis)->field("a.id,a.title,a.price,b.goods_id")->group("a.id")->order("a.top desc,a.id desc")->select()->toArray();
            return $list;
        }
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $service = $data["service"];
        unset($data["service"]);
        $res = $this->insert($data);
        $id = $this->getLastInsID();
        $this->updateSome($id, $data["uniacid"], $service);
        return $id;
    }
    public function dataUpdate($dis, $data)
    {
        if(isset($data["service"])) {
            $service = $data["service"];
            unset($data["service"]);
        }
        $res = $this->where($dis)->update($data);
        if(isset($service)) {
            $this->updateSome($dis["id"], $data["uniacid"], $service);
        }
        return $res;
    }
    public function updateSome($id, $uniacid, $goods)
    {
        $s_model = new CouponService();
        $s_model->where(["coupon_id" => $id])->delete();
        if(!empty($goods)) {
            foreach ($goods as $value) {
                $insert["uniacid"] = $uniacid;
                $insert["coupon_id"] = $id;
                $insert["goods_id"] = $value;
                $s_model->dataAdd($insert);
            }
        }
        return true;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("top desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis, $filed = "*")
    {
        $data = $this->where($dis)->field($filed)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function getDicountPrice($order_goods, $coupon_id, $table_type, $coupon_info_id = 0)
    {
        $coupon_se_model = new CouponService();
        $coupon_record_model = new CouponRecord();
        $table = $coupon_record_model->getServiceTable($table_type);
        $goods_id = \think\facade\Db::name((string) $table)->where(["coupon_id" => $coupon_id, "type" => 1])->column("goods_id");
        $price = 0;
        $service_peice = 0;
        $material_price = 0;
        $send_type = $this->where(["id" => $coupon_info_id])->value("send_type");
        foreach ($order_goods as $v) {
            if(in_array($v["service_id"], $goods_id) || $send_type == 3) {
                $service_peice += $v["all_price"];
                $v["all_price"] += $v["all_material_price"];
                $material_price += $v["all_material_price"];
                $price += $v["all_price"];
            }
        }
        $data["discount"] = $price;
        $data["service_discount"] = $service_peice;
        $data["material_discount"] = $material_price;
        $data["service_id"] = $goods_id;
        return $data;
    }
    public function orderCouponData($order_goods, $coupon_id, $have_discount = 0, $type = 1, $have_full = 0)
    {
        if(empty($coupon_id)) {
            return $order_goods;
        }
        $coupon_record_model = new CouponRecord();
        $info = $coupon_record_model->dataInfo(["id" => $coupon_id]);
        if(empty($info)) {
            return $order_goods;
        }
        if($info["status"] != 1 && $type == 1) {
            return $order_goods;
        }
        if($info["start_time"] < time() && time() < $info["end_time"] || $type == 2) {
            $p_coupon_id = !empty($info["pid"]) ? $info["pid"] : $coupon_id;
            $can_discount_price = $this->getDicountPrice($order_goods["list"], $p_coupon_id, $info["table_type"], $info["coupon_id"]);
            $info["full"] -= $have_full;
            $info["discount"] -= $have_discount;
            if($can_discount_price["discount"] < $info["full"] && $info["type"] == 0 || $type == 2 && $can_discount_price["discount"] <= 0) {
                return $order_goods;
            }
            $info["full"] = $info["type"] == 1 ? 0 : $info["full"];
            $total_discount = $have_discount;
            $total_goods_discount = $total_material_discount = 0;
            $service_discount = $info["full"] <= $can_discount_price["service_discount"] ? $can_discount_price["service_discount"] : $can_discount_price["discount"];
            $send_type = $this->where(["id" => $info["coupon_id"]])->value("send_type");
            foreach ($order_goods["list"] as &$v) {
                if(in_array($v["service_id"], $can_discount_price["service_id"]) || $send_type == 3) {
                    $bin = 0 < $service_discount ? $v["true_price"] / $service_discount : 0;
                    $goods_discount = $bin * $info["discount"] < $v["true_price"] ? $bin * $info["discount"] : $v["true_price"];
                    $material_discount = 0;
                    if($can_discount_price["service_discount"] < $info["full"]) {
                        $sbin = 0 < $service_discount ? $v["true_material_price"] / $service_discount : 0;
                        $material_discount = $sbin * $info["discount"] < $v["true_material_price"] ? $sbin * $info["discount"] : $v["true_material_price"];
                    }
                    $all_discount = $goods_discount + $material_discount;
                    $v["coupon_discount"] = $all_discount;
                    $total_discount += $all_discount;
                    $total_goods_discount += $goods_discount;
                    $total_material_discount += $material_discount;
                    $v["true_price"] = round($v["true_price"] - $goods_discount, 2);
                    $v["true_material_price"] = round($v["true_material_price"] - $material_discount, 2);
                }
            }
            if($total_discount < $info["discount"] && $total_material_discount == 0) {
                $del_discount = $info["discount"] - $total_discount;
                foreach ($order_goods["list"] as &$vv) {
                    if(in_array($vv["service_id"], $can_discount_price["service_id"]) && 0 < $vv["true_material_price"]) {
                        $sbin = $vv["true_material_price"] / $can_discount_price["material_discount"];
                        $material_discount = $sbin * $del_discount < $vv["true_material_price"] ? $sbin * $del_discount : $vv["true_material_price"];
                        $vv["true_material_price"] = round($vv["true_material_price"] - $material_discount, 2);
                        $total_material_discount += $material_discount;
                        $total_discount += $material_discount;
                    }
                }
            }
            $total_discount = $info["discount"] < $info["full"] ? $info["discount"] : round($total_discount, 2);
            $order_goods["total_discount"] = $total_discount;
            $order_goods["total_goods_discount"] = $total_goods_discount;
            $order_goods["total_material_discount"] = $total_material_discount;
            $order_goods["coupon_id"] = $coupon_id;
        }
        return $order_goods;
    }
    public function canUseCoupon($user_id, $coach_id)
    {
        $coupon_model = new CouponRecord();
        $coach_model = new Coach();
        $admin_model = new Admin();
        $coupon_model->where(["user_id" => $user_id, "status" => 1])->where("end_time", "<", time())->update(["status" => 3]);
        $admin_id = $coach_model->where(["id" => $coach_id])->value("admin_id");
        $admin_id = $admin_model->where(["id" => $admin_id, "agent_coach_auth" => 1, "status" => 1])->value("id");
        $admin_id = !empty($admin_id) ? $admin_id : 0;
        $list = $coupon_model->where(["user_id" => $user_id, "status" => 1])->where("admin_id", "in", [0, $admin_id])->order("discount desc,id desc")->select()->toArray();
        $car_model = new Car();
        $car_list = $car_model->carPriceAndCount($user_id, $coach_id, 1);
        $car_list = $car_list["list"];
        $data = [];
        if(!empty($list)) {
            foreach ($list as &$v) {
                if($v["start_time"] < time() && time() < $v["end_time"]) {
                    $id = !empty($v["pid"]) ? $v["pid"] : $v["id"];
                    $info = $this->getDicountPrice($car_list, $id, $v["table_type"], $v["coupon_id"]);
                    if($v["type"] == 1) {
                        $v["full"] = 0;
                    }
                    if($v["full"] <= $info["discount"] && 0 < $info["discount"]) {
                        $data[] = $v["id"];
                    }
                }
            }
        }
        return $data;
    }
}

?>